"""
Case Type   : USTORE
Case Name   : 验证参数recyclebin_retention_time的生效机制
Create At   : 2025/3/10
Owner       : wan005
Description :
    1.查询参数默认值
    2.修改recyclebin_retention_time参数默认值为10
    3.创建ustore表
    4.truncate和drop表后查看回收站
Expect      :
    1.参数默认值15min
    2.修改成功
    3.创建成功
    4.第一次查询无数据，第二次查询有数据
History     :
"""
import os
import time
import unittest
import sys
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant


class Security(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'---{os.path.basename(__file__)} start---')
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.table01 = 't_guc_01'

    def test_ustore(self):
        text = '-----pre1:参数enable_recyclebin设置为on值-----'
        self.logger.info(text)
        pre_res1 = self.common.show_param('enable_recyclebin')
        if pre_res1 == 'off':
            pre_res2 = self.sh_primy.execute_gsguc('reload',
                self.constant.GSGUC_SUCCESS_MSG, 'enable_recyclebin=on')
            self.assertEqual(True, pre_res2, text + '执行失败')

        text = '-----step1:查询参数默认值;expect:参数默认值15min-----'
        self.logger.info(text)
        res1 = self.common.show_param('recyclebin_retention_time')
        self.assertEqual('15min', res1, text + '执行失败')

        text = '----step2:修改参数值为10;expect:成功----'
        self.logger.info(text)
        self.sh_primy.execute_gsguc('reload',
            self.constant.GSGUC_SUCCESS_MSG, 'recyclebin_retention_time=10')
        res2 = self.common.show_param('recyclebin_retention_time')
        self.assertEqual('10s', res2, text + '执行失败')

        text = '-----step3:创建ustore表;expect:创建成功-----'
        self.logger.info(text)
        sql3 = f'create table {self.table01}(a int primary key) ' \
                   f'with(storage_type=ustore);'
        res3 = self.sh_primy.execut_db_sql(sql3)
        self.logger.info(res3)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, res3, text + '执行失败')

        text = '-----step4:truncate和drop表后闪回表;expect:查看到数据-----'
        self.logger.info(text)
        sql4_1 = f'truncate table {self.table01};'
        res4_1 = self.sh_primy.execut_db_sql(sql4_1)
        self.logger.info(res4_1)
        self.assertIn(self.constant.TRUNCATE_SUCCESS_MSG, res4_1,
                      text + '执行失败')
        time.sleep(3)
        sql4_2 = f'timecapsule table {self.table01} to before truncate;'
        res4_2 = self.sh_primy.execut_db_sql(sql4_2)
        self.logger.info(res4_2)
        self.assertEqual('TimeCapsule Table', res4_2, text + '执行失败')
        sql4_3 = f'drop table {self.table01};'
        res4_3 = self.sh_primy.execut_db_sql(sql4_3)
        self.logger.info(res4_3)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, res4_3,
                      text + '执行失败')
        time.sleep(12)
        sql4_4 = f'timecapsule table {self.table01} to before drop;'
        res4_4 = self.sh_primy.execut_db_sql(sql4_4)
        self.logger.info(res4_4)
        self.assertIn(f'ERROR:  recycle object "{self.table01}" desired does '
                      f'not exist or expired', res4_4, text + '执行失败')

    def tearDown(self):
        text = '-----step1:清理环境，恢复配置;expect:成功-----'
        self.logger.info(text)
        sql_cmd = f'purge recyclebin;'
        res1 = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(res1)
        self.assertEqual('PURGE RECYCLEBIN', res1, text + '执行失败')
        self.sh_primy.execute_gsguc('reload', self.constant.GSGUC_SUCCESS_MSG,
                                    'recyclebin_retention_time=15min')
        res2 = self.common.show_param('recyclebin_retention_time')
        self.assertEqual('15min', res2, text + '执行失败')
        self.sh_primy.execute_gsguc('reload', self.constant.GSGUC_SUCCESS_MSG,
                                    'enable_recyclebin=off')
        res3 = self.common.show_param('enable_recyclebin')
        self.assertEqual('off', res3, text + '执行失败')
        self.logger.info(f'---{os.path.basename(__file__)} end---')
